package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.TStudent;
import com.esyoga.yoyoga.pojo.TStudentExample;
import com.esyoga.yoyoga.pojo.TTrainer;
import com.esyoga.yoyoga.pojo.TTrainerExample;
import com.esyoga.yoyoga.service.AddressInfoService;
import com.esyoga.yoyoga.util.GetDistanceByLatLngUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("addressInfoService")
public class AddressInfoServiceImpl implements AddressInfoService {
    @Resource
    TTrainerMapper tTrainerMapper;
    @Resource
    TTrainerExample tTrainerExample;
    @Resource
    TStudentMapper tStudentMapper;
    @Resource
    TStudentExample tStudentExample;

    /**
     * 查询所有与我（该教练）距离相距小于5000m的所有学员信息
     * @param trainer   当前教练
     * @return          小于5000m的所有学员信息
     */
    @Override
    public List<TStudent> getStudentAddressMap(TTrainer trainer,double maxDistance) {
        trainer = tTrainerMapper.selectByPrimaryKey(trainer.gettId());
        String Latlng = trainer.gettLatlng();
        String[] Latlngs1 = Latlng.split(",");
        double lat1 = Double.parseDouble(Latlngs1[0]);
        double lng1 = Double.parseDouble(Latlngs1[1]);
        List<TStudent> allStudents = tStudentMapper.selectByExample(null);
        List<TStudent> students = new ArrayList<>();
        for (TStudent student:allStudents) {
            String[] Latlngs2 = student.getsLatlng().split(",");
            double lat2 = Double.parseDouble(Latlngs2[0]);
            double lng2 = Double.parseDouble(Latlngs2[1]);
            double distance = GetDistanceByLatLngUtil.distance(lat1,lng1,lat2,lng2);
            if (distance<=maxDistance){
                students.add(student);
            }
        }
        return students;
    }

    /**
     * 查询所有与我（该学员）距离相距小于5000m的所有教练信息
     * @param student   当前学员
     * @return          小于5000m的所有教练信息
     */
    @Override
    public List<TTrainer> getTrainersAddressMap(TStudent student,double maxDistance) {
        student = tStudentMapper.selectByPrimaryKey(student.getsId());
        String Latlng = student.getsLatlng();
        String[] Latlngs1 = Latlng.split(",");
        double lat1 = Double.parseDouble(Latlngs1[0]);
        double lng1 = Double.parseDouble(Latlngs1[1]);
        List<TTrainer> allTrainer = tTrainerMapper.selectByExample(null);
        List<TTrainer> trainers = new ArrayList<>();
        for (TTrainer trainer:allTrainer) {
            String[] Latlngs2 = trainer.gettLatlng().split(",");
            double lat2 = Double.parseDouble(Latlngs2[0]);
            double lng2 = Double.parseDouble(Latlngs2[1]);
            double distance = GetDistanceByLatLngUtil.distance(lat1,lng1,lat2,lng2);
            if (distance<=maxDistance){
                trainers.add(trainer);
            }
        }
        return trainers;
    }
}
